import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hinge/common/routers/app_router.dart';
import 'common/global.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Global.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(414, 896),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return MaterialApp.router(
          title: 'Hinge',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
            fontFamily: "PlayfairDisplay",
            splashFactory: NoSplash.splashFactory,
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
          ),

          debugShowCheckedModeBanner: false,

          // 路由配置
          routerConfig: AppRouter.router,

          // 全局性的builder
          builder: (context, widget) {
            // 初始化EasyLoading
            EasyLoading.instance
              ..displayDuration = const Duration(milliseconds: 2000)
              ..indicatorType = EasyLoadingIndicatorType.fadingCircle
              ..loadingStyle = EasyLoadingStyle.custom
              ..indicatorSize = 45.0
              ..radius = 15.0
              ..progressColor = Color(0xFFB197FC)
              ..backgroundColor = Colors.white
              ..indicatorColor = Color(0xFFB197FC)
              ..textColor = Colors.black87
              ..textStyle = TextStyle(
                fontSize: 15.sp,
                fontFamily: 'Inter',
                fontWeight: FontWeight.w500,
                color: Colors.black87,
              )
              ..maskColor = Colors.black.withOpacity(0.3)
              ..userInteractions = true
              ..dismissOnTap = true
              ..successWidget = Container(
                padding: EdgeInsets.all(8),
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.green.shade100,
                ),
                child: Icon(
                  Icons.check_rounded,
                  color: Colors.green.shade700,
                  size: 36,
                ),
              )
              ..errorWidget = Container(
                padding: EdgeInsets.all(8),
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.red.shade100,
                ),
                child: Icon(
                  Icons.close_rounded,
                  color: Colors.red.shade700,
                  size: 36,
                ),
              )
              ..infoWidget = Container(
                padding: EdgeInsets.all(8),
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Color(0xFFD9D2FF),
                ),
                child: Icon(
                  Icons.info_rounded,
                  color: Color(0xFF8C64FF),
                  size: 36,
                ),
              );

            // 套用EasyLoading并设置文本缩放
            widget = EasyLoading.init()(context, widget);

            return MediaQuery(
              data: MediaQuery.of(
                context,
              ).copyWith(textScaler: const TextScaler.linear(1.0)),
              child: widget,
            );
          },
        );
      },
    );
  }
}
